﻿using BaoVeThanhDia.GamePlay.Monsters;
using BaoVeThanhDia.Sound;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Diagnostics;


namespace BaoVeThanhDia.GamePlay.Eff
{
    class MapEffect
    {
        int Type;/* 0 là mưa thiên thạch
                  * 1 là bẫy bom nổ
                  * 2 là đông cứng
                  * 3 là ngưng thời gian
                  */
        Vector2 Index;
        Vector2 Position;
        bool isActive;
        bool isVisible { get; set; }
        Texture2D Image;
        int countDraw;
        float range;
        Random rand;
        int temp;
        int TimeToUseAgain;
        float Dame;
        int countTimeEffect;
        int Mana;
        CustomSoundEffect sound;
        public MapEffect(int type, Vector2 index, int temp = 0, bool isactive = true)
        {
            Type = type;
            Index = index;
            Position = new Vector2(index.Y, index.X) * 128;
            countDraw = 0;
            isVisible = true;
            isActive = isactive;
            if (!isactive && Type == 0) countDraw = 30;
            range = 1;
            rand = new Random();
            if (Type == 3)
            {
                Position = new Vector2(100 + (temp % 6) * 75, 40 + (temp % 6) * 30);
            }
            Dame = 700;
            countTimeEffect = 60;
            this.temp = temp;
            getAssets();
            getUpGradeValue();
        }
        public void Load(ContentManager content)
        {
            //if (sound == null)
            sound.Load(content);
            if (Type == 0)
            {
                if (countDraw < 30) Image = content.Load<Texture2D>("images/effect/MapEffect0/HeadIcon");
                else Image = content.Load<Texture2D>("images/effect/MapEffect0/attack4.info.areaWarning_" + ((countDraw - 30) / 3).ToString());
            }
            else if (Type == 1)
            {
                if (isActive) Image = content.Load<Texture2D>("images/effect/MapEffect1/mine");
                else Image = content.Load<Texture2D>("images/effect/MapEffect1/Bum" + (countDraw / 6).ToString());
            }
            else if (Type == 2)
            {
                if (isActive) Image = content.Load<Texture2D>("images/effect/MapEffect2/ice-block");
                else Image = content.Load<Texture2D>("images/effect/MapEffect2/layer-1");
            }
            else
            {
                Image = content.Load<Texture2D>("images/effect/MapEffect3/Frozen" + (countDraw / 5).ToString());
            }
        }
        public void Draw(SpriteBatch sprite, ContentManager cont)
        {
            if (isVisible)
            {
                Load(cont);
                if (Type == 0)
                {
                    if (countDraw < 30)
                        sprite.Draw(Image, (Position + new Vector2(64, 64)) * GamePage.Scale - new Vector2(100, 100) + GamePage.Dolech, Color.White * (1 - countDraw * 0.03f));
                    else
                    {
                        if (temp == 0) sprite.Draw(Image, (Position + new Vector2(64, 64)) * GamePage.Scale - new Vector2(64, 190) + GamePage.Dolech, Color.White);
                        else if (temp == 1) sprite.Draw(Image, (Position + new Vector2(64, 64)) * GamePage.Scale - new Vector2(64 + 100 * range, 190 + 100 * range) + GamePage.Dolech, Color.White);
                        else if (temp == 2) sprite.Draw(Image, (Position + new Vector2(64, 64)) * GamePage.Scale - new Vector2(64 - 100 * range, 190 + 100 * range) + GamePage.Dolech, Color.White);
                        else if (temp == 3) sprite.Draw(Image, (Position + new Vector2(64, 64)) * GamePage.Scale - new Vector2(64 + 100 * range, 190 - 100 * range) + GamePage.Dolech, Color.White);
                        else if (temp == 4) sprite.Draw(Image, (Position + new Vector2(64, 64)) * GamePage.Scale - new Vector2(64 - 100 * range, 190 - 100 * range) + GamePage.Dolech, Color.White);
                    }
                    countDraw++;
                }
                else if (Type == 1)
                {
                    if (isActive)
                        sprite.Draw(Image, (Position + new Vector2(0, -32)) * GamePage.Scale + GamePage.Dolech, new Rectangle(0, 0, 128, 128), Color.White, 0, Vector2.Zero, GamePage.Scale, SpriteEffects.None, 0);
                    else
                    {
                        sprite.Draw(Image, (Position + new Vector2(64, -20)) * GamePage.Scale - new Vector2(75, 81) + GamePage.Dolech, Color.White);
                        countDraw++;
                    }
                }
                else if (Type == 2)
                {
                    if (isActive)
                        sprite.Draw(Image, (Position + new Vector2(0, -32)) * GamePage.Scale + GamePage.Dolech, new Rectangle(0, 0, 128, 128), Color.White, 0, Vector2.Zero, GamePage.Scale, SpriteEffects.None, 0);
                    else
                    {
                        isVisible = false;
                    }
                }
                else if (Type == 3)
                {
                    sprite.Draw(Image, Position, new Rectangle(0, 0, 200, 117), Color.White, 0, Vector2.Zero, 2f, SpriteEffects.None, 0);
                    countDraw++;
                }
                if (Type == 0 && countDraw >= 70) isVisible = false;
                else if (Type == 1 && countDraw >= 30) isVisible = false;
                else if ((Type == 3 || Type == 2) && countDraw >= 45) isVisible = false;
            }
        }
        public bool getVisible() { return isVisible; }
        public void isActiveEffect()
        {
            if (Type == 1 && isActive)
            {
                #region kích hoạt bom
                foreach (Monster monster in GamePage.monsters)
                {
                    if (isActive && Vector2.Distance(monster.getPosition(), Position) < 128 * GamePage.Scale)
                    {
                        isActive = false;
                    }
                    if (!isActive && Vector2.Distance(monster.getPosition(), Position) < 512 * GamePage.Scale)
                    {
                        monster.setTruMau(1250, 1250, "", 0, 0);
                    }
                }
                #endregion
                if (!isActive) sound.Play();
            }
            else if (Type == 2 && isActive)
            {
                #region kích hoạt băng
                foreach (Monster monster in GamePage.monsters)
                {
                    if (isActive && Vector2.Distance(monster.getPosition(), Position) < 128 * GamePage.Scale)
                    {
                        isActive = false;
                    }
                    if (!isActive && Vector2.Distance(monster.getPosition(), Position) < 512 * GamePage.Scale)
                    {
                        monster.setTruMau(0, 0, "Frozen", 100, countTimeEffect);
                    }
                }
                #endregion
                if (!isActive) sound.Play(0.8f);
            }
            else if (Type == 0 && countDraw > 30)
            {
                #region mưa thiên thạch
                if (countDraw == 60)
                {
                    foreach (Monster monster in GamePage.monsters)
                    {
                        if (Vector2.Distance(monster.getPosition(), Position) < 800 * range * GamePage.Scale)
                        {
                            monster.setTruMau(Dame, Dame, "", 0, 0);
                        }
                    }
                }
                if (countDraw == 35)
                    sound.Play();
                #endregion
            }
            else
            {
                #region ngưng thời gian
                if (countDraw == 40)
                {
                    foreach (Monster monster in GamePage.monsters)
                    {
                        monster.setTruMau(0, 0, "Frozen", 100, countTimeEffect);
                    }
                }
                if (countDraw == 20)
                {
                    sound.Play(0.8f);
                }
                #endregion
            }
        }
        public int getType() { return Type; }
        private void getAssets()
        {
            if (Type == 0)
            {
                sound = new CustomSoundEffect("sounds/effect/Items/Mua");
                Mana = 90;
                TimeToUseAgain = 600;
            }
            else if (Type == 1)
            {
                sound = new CustomSoundEffect("sounds/effect/Items/BomHenGio");
                Mana = 60;
                TimeToUseAgain = 300;
            }
            else if (Type == 2)
            {
                sound = new CustomSoundEffect("sounds/effect/Items/DongCung");
                Mana = 40;
                TimeToUseAgain = 240;
            }
            else if (Type == 3)
            {
                sound = new CustomSoundEffect("sounds/effect/Items/DongCung");
                Mana = 70;
                TimeToUseAgain = 400;
            }
        }
        private void getUpGradeValue()
        {
            if (MapAndUpgrade.File_Upgrade_Star.ItemBought[2])
            {
                // tăng 25% vùng ảnh hưởng mưa sao băng
                if (Type == 0)
                    range += range / 4;
            }
            if (MapAndUpgrade.File_Upgrade_Star.ItemBought[3])
            {
                // tăng 50% sát thương mưa sao băng
                Dame += Dame / 2;
            }
            if (MapAndUpgrade.File_Upgrade_Star.ItemBought[4])
            {
                if (Type == 2)
                    countTimeEffect += 60;
            }
            if (MapAndUpgrade.File_Upgrade_Star.ItemBought[5])
            {
                if (Type == 3)
                    countTimeEffect += 60;
            }
            if (MapAndUpgrade.File_Upgrade_Star.ItemBought[6])
            {
                if (Type == 3 || Type == 2)
                    Mana -= Mana / 2;
            }
        }
        public int getMana() { return Mana; }
        public int getTimeAgain() { return TimeToUseAgain; }
    }
}
